package cn.stylefeng.guns.modular.chain.service;

import cn.stylefeng.guns.core.exceptions.BusinessException;
import cn.stylefeng.guns.modular.app.entity.Member;
import cn.stylefeng.guns.modular.app.service.HomeService;
import cn.stylefeng.guns.modular.app.service.MemberService;
import cn.stylefeng.guns.modular.base.state.Constant;
import cn.stylefeng.guns.modular.base.state.ProConst;
import cn.stylefeng.guns.modular.base.state.F;
import cn.stylefeng.guns.modular.base.util.RedisUtil;
import cn.stylefeng.guns.modular.fin.entity.Wallet;
import cn.stylefeng.guns.modular.fin.service.CurrencyService;
import cn.stylefeng.guns.modular.fin.service.WalletService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import cn.stylefeng.guns.modular.chain.entity.Recharge;
import cn.stylefeng.guns.modular.chain.mapper.RechargeMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充币记录Service
 *
 * @author yaying.liu
 * @Date 2020-01-14 16:00:54
 */
@Service
@Slf4j
public class RechargeService extends ServiceImpl<RechargeMapper, Recharge> {

    @Autowired
    CurrencyService walletService;
    @Autowired
    MemberService memberService;
    @Autowired
    HomeService homeService;

    @Autowired
    RedisUtil redisUtil;


    /**
     * 查询充币记录
     */
    public Page<Map<String, Object>> selectByCondition(Map param) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectByCondition(page, param);
    }

    public List<Recharge> selectByMemberId(Long... memberIds) {

        return this.baseMapper.selectByMemberIds(memberIds);
    }

    ;

    /**
     * 删除充币记录
     */
    public void deleteRecharge(Long rechargeId) {
        Recharge entity = this.baseMapper.selectById(rechargeId);
        //将删除标志设置为Y，表示删除
        entity.setDel("Y");
        this.baseMapper.updateById(entity);
    }

    /**
     * 添加充币记录
     */
    @Transactional
    public Boolean addRecharge(Recharge recharge) {
        try {
            Boolean ok = this.baseMapper.insert(recharge) > 0;

            if (!ok) {
                return false;
            }

            ok = walletService.operateCurrencyUsedPrice(recharge.getMemberId(), "USDT", ProConst.CashFlowOpEnum.FLOW_IN, recharge.getPrice(),"充币");

            if (!ok) {
                throw new BusinessException("充币错误");
            }

            return ok;
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return false;
    }


    public Boolean addRechare(Long memberId, BigDecimal amount, String address, String tx) {
        Recharge recharge = new Recharge();
        recharge.setMemberId(memberId)
                .setOrderNo(cn.stylefeng.guns.modular.base.util.RandomUtil.code("CR"))
                .setRechargeAddress(address)
                .setTxHash(tx)
                .setStatus(ProConst.Status.PASS.code)
                .setPrice(amount)
                .setActualPrice(amount)
                .setCreateUser(memberId);
        return this.addRecharge(recharge);


    }


    public Recharge getUserRecharge(Long memberId){
        LambdaQueryWrapper<Recharge> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Recharge::getMemberId,memberId);
        return this.getOne(lqw,false);
    }


}
